<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="供应商名称" prop="materiaFeedPurchaseProvider">
        <el-input
          v-model="queryParams.provider"
          placeholder="请输入供应商名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="饲料类型" prop="materiaFeedType">
        <el-select
          v-model="queryParams.type"
          placeholder="饲料类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="type in this.typesList"
            :key="type.materiaFeedTypeId"
            :label="type.materiaFeedType"
            :value="type.materiaFeedTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进货时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:purchase:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material:purchase:update']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material:purchase:del']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" prop="materiaFeedPurchaseId" width="60" align="center"/>
      <el-table-column label="饲料类型" prop="materiaFeedType" align="center" width="100"/>
      <el-table-column label="进货日期" align="center" prop="materiaFeedPurchaseDate" width="180">
        <template slot-scope="scope">
          {{ scope.row.materiaFeedPurchaseDate.slice(0, 16) }}
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" prop="materiaFeedPurchaseProvider" :show-overflow-tooltip="true" width="130"/>
      <el-table-column label="数量" prop="materiaFeedPurchaseNum" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ (scope.row.materiaFeedPurchaseNum * scope.row.materiaFeedPurchaseWeight) + ' KG' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" prop="materiaFeedPurchasePrice" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.materiaFeedPurchasePrice + ' 元' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总价" prop="materiaFeedPurchaseTotlePrice" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.materiaFeedPurchaseTotlePrice + ' 元' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="creationDate" width="180">
        <template slot-scope="scope">
          {{ scope.row.creationDate.slice(0, 16) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.materiaFeedPurchaseId !== 0">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material:purchase:update']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material:purchase:del']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改-->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="饲料类型" prop="materiaFeedType">
              <el-select
                v-model="form.materiaFeedType"
                placeholder="请选择饲料类型"
                style="width: 280px"
              >
                <el-option
                  v-for="type in typesList"
                  :key="type.materiaFeedTypeId"
                  :label="type.materiaFeedType"
                  :value="type.materiaFeedTypeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="存储地点" prop="materiaFeedStorePosition">
              <el-select
                v-model="form.materiaFeedStorePosition"
                placeholder="请选择存储地点"
                style="width: 280px"
              >
                <el-option
                  v-for="dict in dict.type.materia_feed_store_position"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="materiaFeedPurchaseProvider">
              <el-input v-model="form.materiaFeedPurchaseProvider" placeholder="请输入供应商名称" style="width: 280px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进货时间" prop="materiaFeedPurchaseDate">
              <el-date-picker
                v-model="form.materiaFeedPurchaseDate"
                placeholder="请选择进货时间"
                style="width: 280px"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数量" prop="materiaFeedPurchaseNum">
              <el-input v-model="form.materiaFeedPurchaseNum" type="number" placeholder="数量(袋)" style="width: 280px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每袋重量" prop="materiaFeedPurchaseWeight">
              <el-input v-model="form.materiaFeedPurchaseWeight" type="number" placeholder="重量(kg)" style="width: 280px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单价" prop="materiaFeedPurchasePrice">
              <el-input v-model="form.materiaFeedPurchasePrice" placeholder="请输入单价(元)" style="width: 280px" @blur="changeTotlePrice"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总价" prop="materiaFeedPurchaseTotlePrice">
              <el-input v-model="form.materiaFeedPurchaseTotlePrice" placeholder="请输入总价(元)" style="width: 280px" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="过期时间" prop="materiaFeedExpireTime">
          <el-date-picker
            v-model="form.materiaFeedExpireTime"
            placeholder="请选择过期时间"
            style="width: 280px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>

import {listMaterial, addPurchase, getPurchase, updatePurchase, delPurchase,
        getTypes} from "@/api/hmy/material";

export default {
  name: "Purchase",
  dicts: ['hmy_material_type', 'materia_feed_store_position'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进货表格数据
      purchaseList: [],
      //饲料类型
      typesList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        provider: undefined,
        dateStart: undefined,
        dateEnd: undefined

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materiaFeedType: [
          {required: true, message: "饲料种类不能为空", trigger: "blur"}
        ],
        materiaFeedPurchaseDate: [
          {required: true, message: "进货时间不可为空", trigger: "blur"}
        ],
        // materiaFeedPurchaseProvider: [
        //   {required: true, message: "供应商不能为空", trigger: "blur"}
        // ],
        materiaFeedPurchaseNum: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        materiaFeedPurchaseWeight: [
          {required: true, message: "重量不能为空", trigger: "blur"}
        ],
        materiaFeedPurchasePrice: [
          {required: true, message: "单价不能为空", trigger: "blur"}
        ],
        materiaFeedPurchaseTotlePrice: [
          {required: true, message: "总价不能为空", trigger: "blur"}
        ],
        materiaFeedStorePosition: [
          {required: true, message: "存储地点不能为空", trigger: "blur"}
        ],
        materiaFeedExpireTime:[
          {required: true, message: "过期时间不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getTypesList();
    this.getList();
  },
  methods: {
    //动态获取饲料数据
    getTypesList(){
      getTypes().then(res => {
        this.typesList = res.data;
      })
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      this.queryParams.dateStart = this.dateRange[0];
      this.queryParams.dateEnd = this.dateRange[1];
      listMaterial(this.queryParams).then(response => {
          this.purchaseList = response.rows;
          this.total = response.total;
          this.purchaseList.forEach(purchaseItem => {
            this.typesList.forEach(typeItem => {
              if (purchaseItem.materiaFeedType == typeItem.materiaFeedTypeId){
                purchaseItem.materiaFeedType = typeItem.materiaFeedType
              }
            })

          })

          this.loading = false;
        }
      );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        materiaFeedType: undefined,
        materiaFeedPurchaseDate: undefined,
        materiaFeedPurchaseProvider: undefined,
        materiaFeedPurchaseNum: undefined,
        materiaFeedPurchaseWeight: undefined,
        materiaFeedPurchasePrice: undefined,
        materiaFeedPurchaseTotlePrice: undefined,
        materiaFeedStorePosition: undefined,
        materiaFeedExpireTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },

    //总价动态生成
    changeTotlePrice(){
      if (this.form.materiaFeedPurchasePrice != undefined && this.form.materiaFeedPurchaseNum != undefined){
        this.form.materiaFeedPurchaseTotlePrice = this.form.materiaFeedPurchasePrice * this.form.materiaFeedPurchaseNum;
      }
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      //this.resetForm("queryForm");
      this.queryParams = {};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materiaFeedPurchaseId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    //添加按钮
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进货信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materiaFeedPurchaseId = row.materiaFeedPurchaseId || this.ids
      getPurchase(materiaFeedPurchaseId).then(response => {
        this.form = response.data;
        this.typesList.forEach(typeItem => {
          if (this.form.materiaFeedType == typeItem.materiaFeedTypeId){
            this.form.materiaFeedType = typeItem.materiaFeedType
          }
        })
        this.open = true;
        this.title = "修改进货信息";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materiaFeedPurchaseId != undefined) {
            this.typesList.forEach(typeItem => {
              if (this.form.materiaFeedType == typeItem.materiaFeedType){
                this.form.materiaFeedType = typeItem.materiaFeedTypeId
              }
            })
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materiaFeedPurchaseIds = row.materiaFeedPurchaseId || this.ids;
      this.$modal.confirm('是否确认删除进货编号为"' + materiaFeedPurchaseIds + '"的数据项？').then(function () {
        return delPurchase(materiaFeedPurchaseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
